<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;


class UserController extends Controller
{
    public function create(){
        return view('admin.add-user');
    }
    public function store(Request $request){

        $user = $request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:6|confirmed',
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，横线-或下划线_，不能包含其他字符',
            'name.min'=>'用户名太短，至少要有:min个字符',
            'name.unique'=>'用户名已存在，不能重复注册',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不合法',
            'email.unique'=>'该邮箱已注册，不能重复注册',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致'
        ];
        $validate = Validator::make($user,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user['password']=Hash::make($user['password']);
        User::create($user);
        return json_encode(['success'=>true,'msg'=>'添加成功']);
    }
    public function edit($id){
            $user=User::find($id);
            return view('admin.edit-user')
                ->with('oUser',$user);
        }

    public function update(Request $request, User $user){
        $datas = $request->all();
        $rules=[
            'password'=>'required|min:6|confirmed',
        ];
        $message=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致'
        ];
        $validate = Validator::make($datas,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user['password']=Hash::make($datas['password']);
        $user->save();
        return json_encode(['success'=>true,'msg'=>'修改成功']);
    }

    public function login(){
        return view('admin.login');
    }
    public function doLogin(Request $request){
        $name=$request->name;
        $password=$request->password;
        if(Auth::attempt(['name'=>$name,'password'=>$password]))
        {
            return json_encode(['success'=>true,'msg'=>'用户登录成功']);
        }
        else{
            return json_encode(['success'=>false,'msg'=>'用户登录错误，请重新登录']);

        }
    }
    public function logout(){
        Auth::logout();
        return redirect('/login');
    }
    public function index(){
        $oUsers=User::orderby('created_at','desc')->paginate(5);
        return view('admin.user-list')
            ->with('oUsers',$oUsers);
    }
    public function destroy($id){
        $user=User::find($id);
        $user->delete();
        return redirect('/adm/user');
    }

}
